<?php
/*
author name: shravani
created date: 15-07-2013
*/
header("Content-type: text/xml");
header('Content-Description: Filtered XML File Transfer');
header('Content-Disposition: attachment; filename=FilteredXMLRecords.xml');
header('Content-Transfer-Encoding: ascii');
header('Expires: 0');
header('Cache-Control: no-cache');

error_reporting( E_ERROR | E_USER_ERROR );
$arrlength = 0;
$xml=simplexml_load_file($_GET['name']);


$isName = FALSE;
$isEmail = FALSE;
$isAddress = FALSE;
$isCity = FALSE;
$isTelephone = FALSE;

if (isset($_GET['is_Name']))
$isName = TRUE;

if (isset($_GET['is_Address']))
$isAddress = TRUE;

if (isset($_GET['is_Email']))
$isEmail = TRUE;

if (isset($_GET['is_City']))
$isCity = TRUE;

if (isset($_GET['is_Telephone']))
$isTelephone = TRUE;

$dataFilterKey = "";
$i = 0;

$strName = "";
$strAddress = "";
$strTelephone = "";
$strCity ="";
$strEmail ="";


foreach($xml->children() as $xml1)
{
	foreach($xml1->children() as $child)
  	{
		    if($child->getName() == "name")
			$strName = $child;
			if($child->getName() == "address")
			$strAddress = $child;
			if($child->getName() == "telephone")
			$strTelephone = $child;
			if($child->getName() == "email")
			$strEmail = $child;
			if($child->getName() == "city")
			$strCity = $child;		
  	}
	if ( $isName OR $isAddress OR $isTelephone OR $isEmail OR $isCity )
	{
			if($isName)
			$dataFilterKey = $dataFilterKey . $strName;
			if($isAddress)
			$dataFilterKey = $dataFilterKey . $strAddress;
			if($isTelephone)
			$dataFilterKey = $dataFilterKey . $strTelephone;
			if($isEmail)
			$dataFilterKey = $dataFilterKey . $strEmail;
			if($isCity)
			$dataFilterKey = $dataFilterKey . $strCity;
			
			
			$dataArray[$dataFilterKey]["name"] = $strName;
			$dataArray[$dataFilterKey]["address"] = $strAddress; 
			$dataArray[$dataFilterKey]["telephone"] = $strTelephone; 
			$dataArray[$dataFilterKey]["email"] = $strEmail; 
			$dataArray[$dataFilterKey]["city"] = $strCity; 
			$dataFilterKey = "";
	}else
	{
					
			$dataArray[$i]["name"] = $strName;
			$dataArray[$i]["address"] = $strAddress; 
			$dataArray[$i]["telephone"] = $strTelephone; 
			$dataArray[$i]["email"] = $strEmail; 
			$dataArray[$i]["city"] = $strCity; 
			$i++;
	}
$doc = new DOMDocument('1.0');

$root = $doc->createElement('data');
$root = $doc->appendChild($root);	
if ( $isName OR $isAddress OR $isTelephone OR $isEmail OR $isCity )
{
	
	foreach($dataArray as $x=>$x_value)
   		{
			  
$record = $doc->createElement('record');
$root->appendChild($record);

$name = $doc->createElement('name');
$record->appendChild($name);
$nametxt = $doc->createTextNode($dataArray[$x]['name']);
$name->appendChild($nametxt);
 
$address = $doc->createElement('address');
$record->appendChild($address);
$addtxt = $doc->createTextNode($dataArray[$x]['address']);
$address->appendChild($addtxt);
 
 $telephone = $doc->createElement('telephone');
 $record->appendChild($telephone);
$phonetxt = $doc->createTextNode($dataArray[$x]['telephone']);
 $telephone->appendChild($phonetxt);
 
 $email = $doc->createElement('email');
 $record->appendChild($email);
$emailtxt = $doc->createTextNode($dataArray[$x]['email']);
 $email->appendChild($emailtxt);
 
 $city = $doc->createElement('city');
 $record->appendChild($city);
$citytxt = $doc->createTextNode($dataArray[$x]['city']);
 $city->appendChild($citytxt);
				
				
   		}
} else
{
	$arrlength=count($dataArray);

	for($x=0;$x<$arrlength;$x++)
  	{
		$record = $doc->createElement('record');
$root->appendChild($record);

$name = $doc->createElement('name');
$record->appendChild($name);
$nametxt = $doc->createTextNode($dataArray[$x]['name']);
$name->appendChild($nametxt);
 
$address = $doc->createElement('address');
$record->appendChild($address);
$addtxt = $doc->createTextNode($dataArray[$x]['address']);
$address->appendChild($addtxt);
 
 $telephone = $doc->createElement('telephone');
 $record->appendChild($telephone);
$phonetxt = $doc->createTextNode($dataArray[$x]['telephone']);
 $telephone->appendChild($phonetxt);
 
 $email = $doc->createElement('email');
 $record->appendChild($email);
$emailtxt = $doc->createTextNode($dataArray[$x]['email']);
 $email->appendChild($emailtxt);
 
 $city = $doc->createElement('city');
 $record->appendChild($city);
$citytxt = $doc->createTextNode($dataArray[$x]['city']);
 $city->appendChild($citytxt);
				

				  		
  }
}

	
}

echo $doc->saveXML();
	
?>

